import axios from "axios";
//引入路由文件
import router from "../../router";
import store from "../../store";

let http = axios.create({
  baseURL: "", 
});

http.interceptors.request.use((req) => {
  console.log(req, "req");
  if (req.url != "/api/userlogin") {
    req.headers.authorization = store.getters.getUserInfo.token;
  }

  return req;
});

//响应拦截器
http.interceptors.response.use((res) => {
  //全局拦截错误信息
  if (res.data.code == 403) {
    alert(res.data.msg);
    router.push("/login");
  }
  return res.data;
});

//导出封装好的新的实例
export default http;
